import 'package:blocks_blkk/page/entity/devcie_entity.dart';
import 'package:blocks_blkk/page/program_entry.dart';
import 'package:blocks_blkk/page/remote_page_20.dart';
import 'package:blocks_blkk/page/remote_page_21.dart';
import 'package:blocks_blkk/page/remote_page_30.dart';
import 'package:blocks_blkk/page/remote_page_40.dart';
import 'package:blocks_blkk/page/remote_page_70.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:yundongjia_ble/yundongjia_ble.dart';

import '../page/app_intl.dart';
import '../page/device_data_context.dart';
import '../page/entity/devcie_entity.dart' as entity;
import '../page/entity/function_button_entity.dart';
import 'function_button.dart';
// import 'gravity_page.dart';

import '../app_configure.dart';
import 'return_button.dart';

// import 'component/function_button.dart';
// import 'component/loading_dialog.dart';
// import 'component/return_button.dart';
// import 'entity/devcie_entity.dart';
// import 'device_data_context.dart';
// import 'entity/function_button_entity.dart';
// import 'instruction_3d.dart';
// import 'program_entry.dart';
// import 'remote_page_20.dart';
// import 'remote_page_21.dart';
// import 'remote_page_30.dart';
// import 'remote_page_40.dart';
// import 'remote_page_60.dart';
// import 'remote_page_70.dart';

/*
 * 功能入口 
 * AppEntry -> FunctionEntry
 */
class FunctionEntry extends StatefulWidget {
  const FunctionEntry({super.key, required this.deviceEntity});
  final DeviceEntity deviceEntity;

  @override
  _FunctionEntryState createState() => _FunctionEntryState();
}

class _FunctionEntryState extends State<FunctionEntry> {
  List<FunctionButtonEntity> get _itemName => [
        FunctionButtonEntity(
          name: AppIntl.getString('remote_txt'),
          image: AppIntl.getString('remote'),
          flag: FunctionButtonEntityEnum.Remoter,
          imageText: '',
        ),
        FunctionButtonEntity(
          name: AppIntl.getString('program_txt'),
          image: AppIntl.getString('program'),
          flag: FunctionButtonEntityEnum.Program,
          imageText: '',
        ),
        // FunctionButtonEntity(
        //   name: AppIntl.getString('gravity'),
        //   image: AppIntl.getString('gravity'),
        //   flag: FunctionButtonEntityEnum.Gravity,
        // ),
        FunctionButtonEntity(
          name: AppIntl.getString('introduce_txt'),
          image: AppIntl.getString('introduce'),
          flag: FunctionButtonEntityEnum.Instruction,
          imageText: '',
        ),
      ];

  @override
  void initState() {
    super.initState();
    DeviceDataContext.instance.switchDevice(widget.deviceEntity.flag);
    //YundongjiaBle.instance.start();
  }

  @override
  void dispose() {
    YundongjiaBle.instance.stop();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: const BoxDecoration(
        image: DecorationImage(
          image: AssetImage("assets/background.png"),
          fit: BoxFit.fill,
        ),
      ),

      //功能入口
      child: Stack(
        children: [
          ReturnButton(),
          //bluetootnBtn(),
          _function(),
        ],
      ),
    );
  }

  //蓝牙图片
  String get _bluetoothImg {
    return AppConfig.instance.bluetoothStatus == 0
        ? 'assets/bluetooth_off.png'
        : 'assets/bluetooth_on.png';
  }

  // Color get _bluetoothShdow {
  //   return AppConfig.instance.bluetoothStatus == 0
  //       ? Colors.transparent
  //       : Colors.transparent;
  // }

  //蓝牙按钮点击事件
  void _onBluetoothBtnTap() {
    setState(() {
      if (AppConfig.instance.bluetoothStatus == 1) {
        AppConfig.instance.bluetoothStatus = 0;
      } else {
        AppConfig.instance.bluetoothStatus = 1;
      }
    });
    if (AppConfig.instance.bluetoothStatus == 1) {
      String message = AppIntl.getString('bluetoot_connect');
      // Navigator.push(context, DialogRouter(LoadingDialog(message)));
      List<int> data = DeviceDataContext.instance.devcie.getPair();
      YundongjiaBle.instance.sendAdvertiseCommand(data);
      Future.delayed(Duration(seconds: 3), () {
        List<int> stopdata = DeviceDataContext.instance.devcie.getStopPair();
        YundongjiaBle.instance.sendAdvertiseCommand(stopdata);
        Navigator.pop(context);
      });
    }
  }

  //蓝牙按钮
  Widget bluetootnBtn() {
    return Positioned(
      left: ScreenUtil().setHeight(200.0),
      top: ScreenUtil().setHeight(40.0),
      width: ScreenUtil().setHeight(150),
      height: ScreenUtil().setHeight(70),
      child: FunctionBtn(
        image: _bluetoothImg,
        onTap: _onBluetoothBtnTap,
        shape: BoxShape.rectangle,
        shadowColor: Colors.transparent,
        backgroundColor: Colors.transparent,
      ),
    );
  }

  Widget _function() {
    return Positioned(
      right: ScreenUtil().setHeight(0),
      left: ScreenUtil().setHeight(0),
      height: ScreenUtil().setHeight(330.0),
      top: ScreenUtil().setHeight(200.0),
      child: Container(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            _itemBuilder(context, 0),
            _itemBuilder(context, 1),
            _itemBuilder(context, 2),
          ],
        ),
        //child: _functionItems(),
      ),
    );
  }

  GestureTapCallback onTap(int index) {
    return () {
      Widget w = pushWidget(context, index);

      Navigator.of(context).push(
        MaterialPageRoute(
          builder: (context) => w,
        ),
      );
    };
  }

  Widget pushWidget(BuildContext context, int index) {
    if (_itemName[index].flag == FunctionButtonEntityEnum.Remoter) {
      if (widget.deviceEntity.flag == entity.DeviceType.Serial2_0) {
        return RemotePage20();
      } else if (widget.deviceEntity.flag == entity.DeviceType.Serial2_1) {
        return RemotePage21();
      } else if (widget.deviceEntity.flag == entity.DeviceType.Serial3_0) {
        return RemotePage30();
      } else if (widget.deviceEntity.flag == entity.DeviceType.Serial4_0) {
        return RemotePage40();
        // } else if (widget.deviceEntity.flag == entity.DeviceType.Serial6_0) {
        //   return RemotePage60();
      } else if (widget.deviceEntity.flag == entity.DeviceType.Serial7_0) {
        return RemotePage70();
        // } else if (widget.deviceEntity.flag == entity.DeviceType.Serial_mp) {
        //   return RemotePage20();
      } else {
        return RemotePage20();
      }
    } else if (_itemName[index].flag == FunctionButtonEntityEnum.Program) {
      return ProgramEntry(deviceEntity: widget.deviceEntity);
    }
    // if (widget.deviceEntity.flag == DeviceType.Serial3_0) {
    //   return ProgramPage(
    //       programEntity: ProgramEntity(name: "programv3"),
    //       htmlName: "index.html");
    // } else if (widget.deviceEntity.flag == DeviceType.Serial6_0) {
    //   return ProgramPage(
    //       programEntity: ProgramEntity(name: "programv6"),
    //       htmlName: "index_6.html");
    // }
    // return ProgramPage(htmlName: "index.html");
    // } else if (_itemName[index].flag == FunctionButtonEntityEnum.Instruction) {
    //   //return InstructionPage(type: DeviceType.Serial4_0);
    //   // if (widget.deviceEntity.flag == DeviceType.Serial4_0) {
    //   //   return InstructionPage(type: DeviceType.Serial4_0);
    //   // }
    //   return Instruction3DPage();
    // } else if (_itemName[index].flag == FunctionButtonEntityEnum.Gravity) {
    //   return GravityPage();
    // }
    //todo 使用说明
    //return RemotePage30();
    // return null;
    return Container();
  }

  Widget _itemBuilder(context, index) {
    return GestureDetector(
        onTap: onTap(index),
        child: Container(
          height: ScreenUtil().setHeight(430.0),
          width: ScreenUtil().setHeight(330.0),
          alignment: const Alignment(0, 1),
          padding: EdgeInsets.only(bottom: ScreenUtil().setHeight(50)),
          decoration: BoxDecoration(
            image: DecorationImage(
              image: AssetImage(_itemName[index].image),
            ),
          ),
          child: Text(
            _itemName[index].name,
            style: TextStyle(
                color: Colors.black,
                fontSize: ScreenUtil().setSp(32),
                decoration: TextDecoration.none),
          ),
        ));
  }
}
